/*
 * ========================================================
 * Copyright(c) 2014 杭州偶尔科技-版权所有
 * ========================================================
 * 本软件由杭州偶尔科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * 
 * 			http://www.kkkd.com/
 * 
 * ========================================================
 */
package com.simon.mengkou.ui.adapter;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.ouertech.android.agm.lib.base.utils.UtilLog;
import com.simon.mengkou.R;
import com.simon.mengkou.data.bean.base.Discovery;
import com.simon.mengkou.data.bean.base.GoodTopic;
import com.simon.mengkou.data.bean.base.Image;
import com.simon.mengkou.data.bean.base.MessageComment;
import com.simon.mengkou.data.bean.base.MessageNotice;
import com.simon.mengkou.data.enums.EEventId;
import com.simon.mengkou.system.global.OuerApplication;
import com.simon.mengkou.system.global.OuerDispatcher;
import com.simon.mengkou.ui.fragment.CategoryFragment;
import com.simon.mengkou.ui.view.CustomViewPager;
import com.simon.mengkou.ui.view.HorizontalListView;
import com.simon.mengkou.utils.UtilImage;
import com.simon.mengkou.utils.UtilOuer;
import com.umeng.analytics.MobclickAgent;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @author : Zhenshui.Xia
 * @since : 2016/3/14.
 * desc    : 发现列表适配器
 */
public class GoodsTopicListAdapter extends AbsAdapter<GoodTopic> {
    public GoodsTopicListAdapter(Context context) {
        super(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {
        final Holder holder;
        if (null == convertView) {
            convertView = mInflater.inflate(R.layout.layout_discovery_item, null);
            holder = new Holder();
            convertView.setTag(holder);
            ButterKnife.bind(holder, convertView);

            holder.mAdapter = new DiscoveryProductAdapter(mContext);
            holder.mHlvList.setAdapter(holder.mAdapter);
        } else {
            holder = (Holder) convertView.getTag();
        }

        final GoodTopic data = getItem(position);

        //设置banner图片
        final Image image = data.getImage();
//        if(image.getHeight() != 0 && image.getWidth() != 0) {
//            holder.mSdvImage.setAspectRatio(image.getWidth() * 1f / image.getHeight());
//        }

        OuerApplication.mImageLoader.loadImage(holder.mSdvImage, image.getUrl());
        holder.mSdvImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MobclickAgent.onEvent(mContext, EEventId.MALL_PRODUCT_TOPIC);
                OuerDispatcher.presentGoodsTopicActivity(mContext, data.getId(), image.getUrl());
            }
        });

        //商品列表
        holder.mAdapter.setList(data.getGoodsArray());
        holder.mHlvList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String pid = data.getGoodsArray().get(position).getId();
                OuerDispatcher.presentProductDetailActivity(mContext, pid);
            }
        });

        return convertView;
    }


    /**
     * @author zhenshui.xia
     */
    class Holder {
        //banner图片
        @Bind(R.id.discovery_id_image)
        SimpleDraweeView mSdvImage;
        //商品列表
        @Bind(R.id.discovery_id_list)
        HorizontalListView mHlvList;
        //商品列表适配器
        DiscoveryProductAdapter mAdapter;
    }
}